/**
 * @author: Lin
 * @create: 2021-04-06 11:22
 * @description: leetcode第一题 TwoSum
 * 示例：
 *          输入：nums = [2,7,11,15], target = 9
 *          输出：[0,1]
 *          解释：因为 nums[0] + nums[1] == 9 ，返回 [0, 1] 。
 **/
import java.util.Arrays;
import java.util.HashMap;

public class TT {
    public static void main(String[] args) {
        int[] nums = {2,7,11,15};
        int target = 9;
        System.out.println(Arrays.toString(twoSum(nums,target)));
    }

    public static int[] twoSum(int[] a,int target){

        int[] result = new int[2];
        HashMap<Integer,Integer> map = new HashMap<>();

        for (int i = 0; i < a.length; i++) {
            if (map.containsKey(a[i])){
                result[0] = map.get(a[i]);
                result[1] = i;
                break;
            }map.put((target - a[i]), i);
        }

        return result;
    }
}
